#ifndef __DYNAMICLIST_H__
#define __DYNAMICLIST_H__

#include "Common.h"
#include "SeqList.h"

NAME_SPACE_BEGIN

template<typename T>
class DynamicList : public SeqList<T>
{
public:
	using base_List_type	= List<T>;

	using value_type		= base_List_type::value_type;
	using pointer			= base_List_type::pointer;
	using const_pointer		= base_List_type::const_pointer;
	using reference			= base_List_type::reference;
	using const_reference	= base_List_type::const_reference;

public:
	DynamicList(int capacity) {
		this->m_array = new value_type[capacity];
		if (nullptr != this->m_array)
		{
			this->m_length = 0;
			this->m_capacity = capacity;
		}
		else {
			THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create DynamicList object ...");
		}
	}

	int capacity() const {
		return m_capacity;
	}

	void resize(int capacity) {
		if (capacity != m_capacity)
		{
			pointer array = new value_type[capacity];
			if (nullptr != array)
			{
				int length = (this->m_length < capacity ? this->m_length : capacity);
				for (int i = 0; i < length; i++)
				{
					array[i] = this->m_array[i];
				}

				pointer temp = this->m_array;
				this->m_array = array;
				this->m_length = length;
				this->m_capacity = capacity;
				delete[] temp;
			}
			else {
				THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create DynamicList object ...");
			}
		}
	}

	~DynamicList() {
		delete[] this->m_array;
	}

protected:
	int m_capacity;
};

NAME_SPACE_END

#endif
